package edu.zzuli.common;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.zzuli.model.core.iface.PublicFacade;
import edu.zzuli.util.SpringBeanUtil;

/**
 * @author tianshaojie
 * @date 2011-8-19
 * @discription : AppSessionListener
 */
public class AppSessionListener implements HttpSessionListener {
	
	Log log = LogFactory.getLog(this.getClass());

	private static int sessionCount = 0;

	public void sessionCreated(HttpSessionEvent httpSessionEvent) {
		sessionCount++;
		httpSessionEvent.getSession().setAttribute("sessionCount", String.valueOf(sessionCount));
		log.info("[sessionCreated]sessionCount=" + sessionCount);
	}

	public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
		Visit myVisit = (Visit) httpSessionEvent.getSession().getAttribute("myVisit");
		if (myVisit != null && myVisit.getCzy()!= null && myVisit.getGG_JGBH()!= null) {
			try {
				Map mapPara = new HashMap();
				mapPara.put("SFDL", 0);
				mapPara.put("ID", myVisit.getCzy().getId());
				this.getPublicFacade().execute("update gg_czyb set sfdl=:SFDL where id=:ID",mapPara);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else {
			log.info("session已失效！");
		}
		if (sessionCount > 0) {
			sessionCount--;
			httpSessionEvent.getSession().setAttribute("sessionCount", String.valueOf(sessionCount));
			log.info("[sessionDestroyed]sessionCount=" + sessionCount);
		}
	}
	
	private PublicFacade getPublicFacade() {
		return (PublicFacade) SpringBeanUtil.getBean("publicFacade");
	}
}
